# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=libgda
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.0.0
pkgrel=1
pkgdesc="GDA (GNOME Data Access) library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gnome-db.org/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/libgda"
msys2_references=(
  'archlinux: libgda'
  "cpe: cpe:/a:gnome:libgda"
)
license=("apdx:LGPL-2.0-only AND GPL-2.0-only")
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-gtksourceview3"
         "${MINGW_PACKAGE_PREFIX}-goocanvas"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-libsecret"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-glade"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-iso-codes"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             #"${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-openldap"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "intltool")
optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql"
            #"${MINGW_PACKAGE_PREFIX}-libmariadbclient"
            "${MINGW_PACKAGE_PREFIX}-openldap")
source=(https://download.gnome.org/sources/${_realname}/${pkgver:0:3}/${_realname}-${pkgver}.tar.xz
        001-lemon.patch
        002-fix-setup-with-latest-meson.patch
        003-fix-build-on-mingw.patch
        006-Use-goocanvas-3.0.patch
        008-lemon-fix-segfault.patch)
sha256sums=('995f4b420e666da5c8bac9faf55e7aedbe3789c525d634720a53be3ccf27a670'
            'e61c10e41ea2b343c6a50d0cbf1f2912b9d9273882249a998f60cda2f4a94d4c'
            '8db5e73f873ddbc811b691afe6b8715788c323e0f623cc4dc5d5ace20bde575a'
            'ac491bf4a61ae5d813a94a24482472c307ef120d5c9164e3f2227d6eb350d142'
            'd77c3c25273a7db4eafd292a8c7fb30a5a8d1c3c6baffc45457d1aacb52fdf68'
            '677ac2d5b6936e8eaf676c0a79027065c6f41d8f46d385c56ad5b716a1367034')
noextract=("${_realname}-${pkgver}.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.xz ..."
  tar -xJf ${_realname}-${pkgver}.tar.xz || true

  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-lemon.patch \
    002-fix-setup-with-latest-meson.patch \
    003-fix-build-on-mingw.patch

  # https://github.com/archlinux/svntogit-community/tree/packages/libgda/trunk
  apply_patch_with_msg \
    006-Use-goocanvas-3.0.patch

  # https://gitlab.gnome.org/GNOME/libgda/-/merge_requests/176
  apply_patch_with_msg \
    008-lemon-fix-segfault.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  CFLAGS+=" -Wno-int-conversion -Wno-incompatible-function-pointer-types -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="--prefix=;-Dglade-catalog-dir=;-Dglade-pixmap-dir" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    --libexecdir=${MINGW_PREFIX}/lib \
    "${_extra_config[@]}" \
    -Dglade-catalog-dir=${MINGW_PREFIX}/share/glade/catalogs \
    -Dglade-pixmap-dir=${MINGW_PREFIX}/share/glade/pixmaps \
    -Dgraphviz=true \
    -Dlibsecret=true \
    -Dtools=true \
    -Ddoc=true \
    -Dexperimental=true \
    -Dldap=true \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -i -e "s|${PREFIX_WIN}|\$\{prefix\}|g" -e "3,5s|${MINGW_PREFIX}|\$\{prefix\}|g" "${pcfile}"
  done
  sed -i -e "38,42s|${MINGW_PREFIX}|\$\{prefix\}|g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/libgda-sqlite-6.0.pc

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $srcdir/${_realname}-${pkgver}/COPYING* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}
